package com.freestyle.sentinel;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by rocklee on 2022/3/28 16:33
 */

public class MethodArgs implements Serializable {
  @JsonIgnore
  private transient Method method;
  @JsonProperty("method")
  private Map<String,Object> methodDescribe;
  private Object[] args;
  private String returnClass;

  public MethodArgs(Method method, Object[] args, Type returnType) {
    this();
    setMethod(method);
    this.args = args;
    this.returnClass = returnType.toString();
  }

  public MethodArgs() {
    methodDescribe=new LinkedHashMap<>();
  }

  public Method getMethod() {
    return method;
  }

  public void setMethod(Method method) {
    this.method = method;
    methodDescribe.clear();
    methodDescribe.put("name",method.toString());
    methodDescribe.put("genericParameterTypes", "["+ Arrays.stream(method.getGenericParameterTypes()).map(m->m.toString()).collect(Collectors.joining(","))+"]");
    methodDescribe.put("genericReturnType",method.getGenericReturnType().toString());
    //node.put("returnType",method.getReturnType().toString());
  }

  public Map<String,Object> getMethodDescribe() {
    return methodDescribe;
  }

  public void setMethodDescribe(Map<String,Object> methodDescribe) {
    this.methodDescribe = methodDescribe;
  }

  public Object[] getArgs() {
    return args;
  }

  public void setArgs(Object[] args) {
    this.args = args;
  }

  public String getReturnClass() {
    return returnClass;
  }

  public void setReturnClass(String returnClass) {
    this.returnClass = returnClass;
  }
}
